package com.qf.utils;


import com.qf.pojo.resp.BaseResp;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.internet.MimeMessage;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author guoxu
 * @create 2021/3/5 9:05
 */
@Component
public class SendUtils {
    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String from;

    /***
     * Desc: 发送手机验证码并且存入redis
     * @param phone 手机号码
     * @return BaseResp
     * @author 郭旭
     * @date 2021/3/5 9:10
     */
    public BaseResp sendPhone(String phone) {
        try {
            Credential cred = new Credential("AKIDDDyB9qnAYfcEk8PIXWt4zF4tNefQba2F", "2oMdeOuCKWaQ0JODo9DguswlttcAIuxw");
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setReqMethod("POST");
            httpProfile.setConnTimeout(60);
            httpProfile.setEndpoint("sms.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setSignMethod("HmacSHA256");
            clientProfile.setHttpProfile(httpProfile);
            SmsClient client = new SmsClient(cred, "",clientProfile);
            SendSmsRequest req = new SendSmsRequest();
            String appid = "1400486894";
            req.setSmsSdkAppid(appid);
            String sign = "总想挂三挡";
            req.setSign(sign);
            String templateID = "874902";
            req.setTemplateID(templateID);
            String[] phoneNumbers = {"+86"+phone};
            req.setPhoneNumberSet(phoneNumbers);
            Random random = new Random();
            StringBuffer stringBuffer = new StringBuffer();
            for (int i = 0;i<4;i++){
                stringBuffer.append(random.nextInt(10));
            }
            String[] templateParams = {stringBuffer.toString(),"5"};
            req.setTemplateParamSet(templateParams);
            SendSmsResponse res = client.SendSms(req);
            redisTemplate.opsForValue().set(phone,stringBuffer.toString(),360, TimeUnit.SECONDS);

        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        BaseResp baseResp =new BaseResp();
        baseResp.setMessage("发送成功");
        return baseResp;
    }

    /***
     * Desc: 验证码发送
     * @param email 前端的邮箱
     * @return BaseResp
     * @author 郭旭
     * @date 2021/3/5 9:21
     */
    public BaseResp sendEmail(String email) {
        BaseResp baseResp = new BaseResp();
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = null;
        try{
            mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setTo(email);
            mimeMessageHelper.setSubject("注册验证码");

            Random random = new Random();
            StringBuffer stringBuffer = new StringBuffer();
            for (int i = 0;i<4;i++){
                stringBuffer.append(random.nextInt(10));
            }
            mimeMessageHelper.setText("<font style='color:red'>注册验证码为：</font>"+stringBuffer.toString(),true);
            javaMailSender.send(mimeMessage);
            redisTemplate.opsForValue().set(email,stringBuffer.toString(),360, TimeUnit.SECONDS);
            baseResp.setCode(200);
            baseResp.setMessage("邮件发送成功");

        }catch (Exception e){
            e.printStackTrace();
            baseResp.setCode(203);
            baseResp.setMessage("邮件发送失败");
        }

        return baseResp;

    }

    /***
     * Desc: 验证短信或者邮箱验证码是否和redis中的一致
     * @param   way 是手机或者邮箱号码 code是验证码
     * @return  布尔值  false 或者  true
     * @author  郭旭
     * @date 2021/3/5 9:11
     */
    public Boolean validation(String way, String code) {

        String o = (String) redisTemplate.opsForValue().get(way);
        if (o != null && o.equals(code)) {
            return true;
        } else {
            return false;
        }
    }
}
